﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace 教师信息管理系统
{
    public partial class teacherinfo : Form
    {
        public teacherinfo()
        {
            InitializeComponent();
        }
        public teacherinfo(string s):this()
        {
           label2.Text = s;
        }

        private void teacherinfo_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“teacherDataSet.teacherinfo”中。您可以根据需要移动或删除它。
            this.teacherinfoTableAdapter.Fill(this.teacherDataSet.teacherinfo);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.teacherinfoBindingSource.Filter = "usersid='" + textBox1.Text + "'";//筛选从绑定表的数据源中符合条件的数据
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.teacherinfoTableAdapter.Update(this.teacherDataSet.teacherinfo);//更新teacherinfo表中的数据
            MessageBox.Show("修改成功！");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = this.saveFileDialog1.FileName;
                FileStream fs = File.Open(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                SqlConnection conn = new SqlConnection("server=.;database=teacher;integrated security=true;");
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from teacherinfo";
                SqlDataReader reader = cmd.ExecuteReader();
                int i = 0;
                while (reader.Read())
                {
                    string s = reader[0].ToString() + "\t" +
                        reader[1].ToString() + "\t" +
                        reader[2].ToString() + "\t" +
                        reader[3].ToString() +"\t"+
                        reader[4].ToString();
                        sw.WriteLine(s);
                    i++;
                }
                reader.Close();
                conn.Close();
                sw.Close();
                fs.Close();

                MessageBox.Show("已保存到文件" + filename + "，总共" + i + "条记录");


            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“teacherDataSet.teacherinfo”中。您可以根据需要移动或删除它。
            this.teacherinfoTableAdapter.Fill(this.teacherDataSet.teacherinfo);
        }
    }
}
